mod args_parser;
mod display;
mod history_file;

use args_parser::Args;
use clap::Parser;
use std::{env, path::PathBuf};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();
    let file_path = match args.file.as_ref() {
        Some(p) => p.to_owned(),
        // config here, export USEFILE=.bashrc or some other
        None => match env::var("USEFILE") {
            Ok(p) => PathBuf::from(p),
            Err(_e) => {
                return Err("Please set USEFILE variavle at your shell environment, do like: export USEFILE=$HOME/.bash_history, or do pass --file option".into());
            }
        },
    };

    let file = std::fs::File::open(file_path).expect("failed to get the must provide file");

    let content = history_file::get_content(file, &args);
    let command_line = history_file::parse_content(content, &args);
    let command = history_file::process_line(command_line, &args);

    display::print(command, args);

    Ok(())
}
